<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-row type="flex" justify="end">
        <el-button
          size="small"
          type="primary"
          icon="el-icon-edit"
          @click="addPermission(1, '0')"
        >添加菜单</el-button>
      </el-row>
      <el-table border :data="list" row-key="id">
        <el-table-column label="菜单名称" prop="name" />
        <el-table-column align="center" label="权限标识" prop="code" />
        <el-table-column align="center" label="描述" prop="description" />
        <el-table-column align="center" label="操作">
          <template slot-scope="{row}">
            <el-button v-if="row.type === 1" type="text" @click="addPermission(2, row.id)">添加权限点</el-button>
            <el-button type="text">查看api权限</el-button>
            <el-button type="text" @click="editPermission(row.id)">编辑</el-button>
            <el-button type="text" @click="delPermission(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 弹窗 -->
      <el-dialog :title="title" :visible="showDialog" @close="btnCancel">
        <el-form ref="formData" :model="formData" :rules="rules" label-width="120px">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item v-model="formData.description" label="权限描述" prop="description">
            <el-input />
          </el-form-item>
          <el-form-item label="开启" prop="enVisible">
            <el-switch v-model="formData.enVisible" active-value="1" inactive-value="0" />
          </el-form-item>
        </el-form>
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" type="primary" @click="btnOK">确定</el-button>
            <el-button size="small" @click="btnCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getPermissionList, delPermission, addPermission, getPermissionDetail, updatePermission } from '@/api/permisson'
import { tranListToTreeData } from '@/utils/index'
export default {
  // 组件名称
  name: '',
  // 局部注册的组件
  components: {},
  // 组件参数 接收来自父组件的数据
  props: {},
  // 组件状态值
  data() {
    return {
      list: [],
      formData: {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 开启
      },
      showDialog: false,
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      }
    }
  },
  // 计算属性
  computed: {
    title() {
      return this.formData.id ? '权限编辑' : '添加权限'
    }
  },
  // 侦听器
  watch: {},
  created() {
    this.getPermissionList()
  },
  // 组件方法
  methods: {
    // 获取权限列表
    async getPermissionList() {
      this.list = tranListToTreeData(await getPermissionList(), '0')
    },
    // 根据id删除
    async delPermission(id) {
      await this.$confirm('确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      await delPermission(id)
      this.$message.success('删除成功')
      this.getPermissionList()
    },
    // 添加
    addPermission(type, pid) {
      // 访问权type=1 按钮操作权type=2
      // pid表示当前数据父节点的标识
      // 记录当前添加的类型和父标识
      this.formData.type = type
      this.formData.pid = pid
      this.showDialog = true
    },
    // 点击确定
    async btnOK() {
      await this.$refs.formData.validate()
      if (this.formData.id) {
        // 根据id判断是编辑还是添加  有就是编辑 否则添加
        await updatePermission(this.formData)
        this.$message.success('编辑成功')
      } else {
        await addPermission(this.formData)
        this.$message.success('添加成功')
      }
      this.getPermissionList()
      this.showDialog = false
    },
    // 点击取消
    btnCancel() {
      this.formData = {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 开启
      }
      this.$refs.formData.resetFields()
      this.showDialog = false
    },
    // 根据id获取权限详情 并回填到formData
    async editPermission(id) {
      this.showDialog = true
      this.formData = await getPermissionDetail(id)
    }
  }
}
</script>

<style scoped lang="less"></style>
